var Pproductdescription = "";
var Poutput = "";
var Pname = "";
var Pbuyurl = "";
var enumCategoryid = "";
var Punitprice = "";
var Punit = "";
var Farmname = "";
var show = "";
var tuijian = "";
var classifyId = "";
var cpid = "";
var productImages = "";
var productOld = {};
$(document).ready(function () {
//    if (AUTH.role != "vimi8_com" && AUTH.job != "vimi_manager") {
//        ShowTip("没有管理农场的权限，请用为米指定账户登录！", "success", 1000);
//        setTimeout(function () {
//            window.location = "login.html";
//        }, 1000);
//    }
if(orgClass!="org_ebb_entity"){
    ShowTip("你还没有权限做该操作！请联系管理员", "success", 1000);
     setTimeout(function () {
                                            window.location = "productManager.html";
                                        }, 1000);

}
    startReadyClassify();
    var productId = GetQueryString("id");
    cpid = productId;
    GetFarmByFarmId(productId);
    selectCate();
});

//更新产品
function updateProducts() {

    var productName = $("#name").val();
    var CategoryId = $("#category").text();
    var salesArea = $("#salesArea").val();
    var businessVolume = $("#businessVolume").val();
    var tradingVolume = $("#tradingVolume").val();
    var tradingTime = $("#tradingTime").val();
    var unitPrice = $("#unitPrice").val();
    var stockVolume = $("#stockVolume").val();
    var stockUnit = $("#stockUnit").val();

    if(productName==""){
        productName = productOld.productName;
    }
    if(CategoryId==""){
        CategoryId = productOld.enumCategoryId;
    }
    if(salesArea==""){
        salesArea = productOld.salesArea;
    }
    if(businessVolume==""){
        businessVolume = productOld.businessVolume;
    }
    if(tradingVolume==""){
        tradingVolume = productOld.tradingVolume;
    }
    if(tradingTime==""){
        tradingTime = productOld.tradingTime;
    }
    if(unitPrice==""){
        unitPrice = productOld.unitPrice;
    }
    if(stockVolume==""){
        stockVolume = productOld.stockVolume;
    }
    if(stockUnit==""){
        stockUnit = productOld.stockUnit;
    }

    var params = {
        "businessVolume": businessVolume,
        "electricityBusinessId": "",
        "electricityBusinessName": "",
        "enumCategoryId": CategoryId,
        "enumCategoryName": "",
        "id":cpid,
        "productName": productName,
        "salesArea": salesArea,
        "stockUnit": stockUnit,
        "stockVolume":stockVolume ,
        "tradingTime":tradingTime ,
        "tradingVolume": tradingVolume,
        "unitPrice": unitPrice,
    }
    $.ajax({
        type: "PUT",
        url: "/ebb/entity/product/",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (result) {
            ShowTip("修改产品成功", "success", 1500);
            setTimeout(function () {
                window.location = "productManager.html";
            }, 2000);
        },
        error: function (err) {
            console.log(err);
            ShowTip("修改产品失败", "danger", 1500);//alert-success、.alert-info、.alert-warning、.alert-danger
        }
    });
}

//获取产品分类
function startReadyClassify(){
    $.ajax({
        type: "get",
        url: "/ebb/category/?id=1200000000&level=1",
        dataType: "json",
        async: true,
        success: function (data) {
            var result = data;
            $("#oneClassify").html($.templates("#oneClassifyTemp").render(result));
        }
    });
}

function selectCate() {
   var _el = $(".secect-cate-level select");
   _el.on("change", function() {
       var changeClassify = $(this).children('option:selected').val();
       var arr=changeClassify.split(",");
       var get = this;
       var count = parseInt(arr[1])+1;
       $.ajax({
               type: "get",
               url: "/ebb/category/?id="+arr[0]+"&level="+count,
               dataType: "json",
               async: true,
               success: function (data) {
                   if(data.length > 0){
                       var result = data;
                       $(get).parent().next().children("select").html("");
                       for(var i=0;i<result.length;i++){
                           $(get).parent().next().children("select").append("<option value=\""+result[i].id+','+result[i].categoryLevel+"\">"+result[i].categoryName+"</option>");
                       }
                       $(get).parent().next().nextAll().children("select").html("");
                       $("#selectCatecomfirm").addClass("disabled");
                   }else {
                       $("#selectCatecomfirm").removeClass("disabled");
                   }
               }
           });
   });
}
function makeSureClassify(){
   var oneClassify = $("#oneClassify").children('option:selected').val();
   var twoClassify = $("#twoClassify").children('option:selected').val();
   var threeClassify = $("#threeClassify").children('option:selected').val();
   var fourClassify = $("#fourClassify").children('option:selected').val();
   var oneClassify1 = $("#oneClassify").children('option:selected').text();
   var twoClassify1 = $("#twoClassify").children('option:selected').text();
   var threeClassify1 = $("#threeClassify").children('option:selected').text();
   var fourClassify1 = $("#fourClassify").children('option:selected').text();
   $("#categrey").html(oneClassify1+"/"+twoClassify1+"/"+threeClassify1+"/"+fourClassify1);
   $("#selectCateModal").modal("hide");
    var arr;
    if(fourClassify != undefined){
        arr=fourClassify.split(",");
    }else if(fourClassify == undefined && threeClassify != undefined){
        arr=threeClassify.split(",");
    }else if(threeClassify == undefined && twoClassify != undefined){
        arr=twoClassify.split(",");
    }else if(twoClassify == undefined && oneClassify != undefined){
        arr=oneClassify.split(",");
    }
   classifyId = arr[0];
   classifyIdUpdata = arr[0];
   $("#category").text(classifyId);
}

var farmname = "";
var page = 1 ;
var size = 10 ;
var nctotalPages = 0;
//修改时获取农场信息
function comments(){
    farmname= $("#farmName").val().trim();
    getfarm();
}
function getfarm(){
    var ncpost ={
        "enumCategoryId": "",
        "farmName": farmname,
        "index": page,
        "size": size,
    }
    $.ajax({
        type: "post",
        url: "/industrial/farm/farmInfo/",
        data: JSON.stringify(ncpost),
        contentType: "application/json",
        success: function (data) {
            var result = data.data;
            $("#farmInfoList").html($.templates("#farmInfoTemp").render(result));
            nctotalPages = data.totalPages;
            pageFarmData(nctotalPages);
        }
    });
}
function pageFarmData(nctotalPages){
    var totalCount = nctotalPages;
    $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
        $('#pagination').twbsPagination({
            totalPages: totalCount,
            visiblePages: 5,
            first: '<<',
            prev: '<',
            next: '>',
            last: '>>',
            onPageClick: function (event, page) {
                var ncpost ={
                    "enumCategoryId": "",
                    "farmName": farmname,
                    "index": page,
                    "size": size,
                }
                $.ajax({
                    type: "post",
                    url: "/industrial/farm/farmInfo/",
                    data: JSON.stringify(ncpost),
                    contentType: "application/json",
                    success: function (data) {
                        var result = data.data;
                        $("#farmInfoList").html($.templates("#farmInfoTemp").render(result));
                    }
                });
            }
        });
}


var farmName = "";
var addrsss = "";
var classifyId = "";
function selectPoor(get) {
    $("#selectPoorModal").modal("hide");
    $('#newProduct').collapse('hide');
    $("#newProduct").modal("hide");
    farmName = $(get).parent("td").parent("tr").children("td:eq(0)").text();
//    phones = $(get).parent("td").parent("tr").children("td:eq(2)").text();
//    addrsss = $(get).parent("td").parent("tr").children("td:eq(3)").text();
    ids = $(get).parent("td").parent("tr").children("td:eq(2)").text();
    $("#poorSet").html(farmName);
    $("#farmId").html(ids);
}


//修改产品之前获取产品信息
function GetFarmByFarmId(productId){
    $.ajax({
        type: "get",
        url: "/ebb/entity/productinfo/?id="+productId,
        dataType: "json",
        async: true,
        success: function(data) {
            if(data!=null&&data!=""){
                $("#name").val(data.productName);
                $("#category").html(data.enumCategoryId);
                $("#categrey").html(data.enumCategoryName);
                $("#salesArea").val(data.salesArea);
                $("#businessVolume").val(data.businessVolume);
                $("#tradingVolume").val(data.tradingVolume);
                $("#tradingTime").val(data.tradingTime);
                $("#unitPrice").val(data.unitPrice);
                $("#stockVolume").val(data.stockVolume);
                $("#stockUnit").val(data.stockUnit);
                productOld = data;
            }
        },
        error:function (err) {
            console.log("获取产品出错！");
        }
    });
}
